package com.yeskery.nut.core;

import com.yeskery.nut.util.IOUtils;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
 * 基于内存的缓存资源对象
 * @author YESKERY
 * 2025/9/2
 */
public class ByteBufferedResource implements Resource {

    /** 资源名称 */
    private final String name;

    /** 缓存字节数组 */
    private final byte[] bytes;

    /**
     * 构建基于内存的缓存资源对象
     * @param resource 资源对象
     */
    public ByteBufferedResource(Resource resource) {
        name = resource.getName();
        bytes = IOUtils.readByteArray(resource.getInputStream());
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public Long length() {
        return (long) bytes.length;
    }

    @Override
    public InputStream getInputStream() {
        return new ByteArrayInputStream(bytes);
    }
}
